package com.king.readwritelock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author taomk
 * @version 1.0
 * @since 15-2-26 下午10:17
 */
public class ReadWriteLockTest {

	private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

	private static Lock readLock = readWriteLock.readLock();

	private static Lock writeLock = readWriteLock.writeLock();

	public static void main(String [] args) throws InterruptedException {
		Thread read = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					readLock.lock();
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					readLock.unlock();
				}
			}
		});

		Thread write = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					writeLock.lock();
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					writeLock.unlock();
				}
			}
		});

		read.start();
		write.start();
	}
}
